﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FinalProject.DAL.Model;
using System.Collections;

namespace FinalProject.BL
{
    public class DeliveryManagementBL : BaseBL<FinalProject.DAL.Model.DeliveryDocument>
    {
        public static Guid LoadDeliveryDefaultJournal()
        {
            try
            {
                var query = from jrlType in dalContext.JournalTypes
                            join
                                jrl in dalContext.Journals on jrlType.Id equals jrl.JournalType.Id
                            where jrlType.Code == "DELIV"
                            select new { Id = jrl.Id };
                if (query.FirstOrDefault() != null)
                    return query.FirstOrDefault().Id;
                else
                    return Guid.Empty;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public static long GetLastestNumber()
        {
            try
            {
                long result = dalContext.DeliveryDocuments
                                        .Max(p => p.DocNumber) ?? 0;

                return result + 1;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public static IList<DeliveryRowEntry> GetAllRowEntryByOrder(Guid orderId)
        {
            try
            {
                var subQuery = from delivery in dalContext.DeliveryDocuments
                               join deliveryRow in dalContext.DocumentRows on delivery.Id equals deliveryRow.DeliveryDocumentId
                               where delivery.OrderDocumentId == orderId
                               select deliveryRow.Quantity;

                var query = from orderRow in dalContext.DocumentRows
                            where orderRow.OrderDocumentId == orderId
                            select new DeliveryRowEntry()
                            {
                                RowNumber = orderRow.RowNumber ?? 0,
                                ItemName = orderRow.Item.Name,
                                ItemId = orderRow.ItemId,
                                WarehouseName = orderRow.Item.Warehouse.Code,
                                OrderQuantity = orderRow.Quantity,
                                DeliveriedQuantity = (from delivery in dalContext.DeliveryDocuments
                                                      join deliveryRow in dalContext.DocumentRows on delivery.Id equals deliveryRow.DeliveryDocumentId
                                                      where delivery.OrderDocumentId == orderId && deliveryRow.RowNumber == orderRow.RowNumber
                                                      select deliveryRow.Quantity).Sum(),
                                UnitPrice = orderRow.UnitPrice ?? decimal.Zero
                            };
                IList<DeliveryRowEntry> result = query.ToList();


                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public static bool InsertOrUpdate(DeliveryDocument header, IList<DeliveryRowEntry> lstDetail)
        {
            try
            {
                if (header.Id == Guid.Empty)
                    dalContext.DeliveryDocuments.AddObject(header);
                foreach (var item in lstDetail)
                {
                    if (item.RowId == Guid.Empty)
                    {
                        DocumentRow newRow = new DocumentRow()
                        {
                            ItemId = item.ItemId,
                            RowNumber = item.RowNumber,
                            UnitPrice = item.UnitPrice,
                            Quantity = item.Quantity,
                            TotalAmount = item.TotalAmount,
                            Description = item.Description,
                            OrderDocumentId = null,
                            InvoiceDocumentId = null
                        };
                        header.DocumentRows.Add(newRow);
                    }
                    else
                    {
                        DocumentRow existRow = BaseBL<DocumentRow>.LoadById(item.RowId);
                        existRow.Quantity = item.Quantity;
                        existRow.Description = item.Description;
                    }
                }
                dalContext.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static IList GetDeliveryForLookup()
        {
            try
            {
                var query = from deli in dalContext.DeliveryDocuments
                            join
                                cust in dalContext.Customers on deli.CustomerId equals cust.Id
                            select new
                            {
                                Id = deli.Id,
                                Number = deli.DocNumber,
                                CustomerCode = cust.Code
                            };
                return query.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }

    public class DeliveryRowEntry
    {
        public Guid RowId { get; set; }
        public int RowNumber { get; set; }
        public Guid ItemId { get; set; }
        public string ItemName { get; set; }
        public string WarehouseName { get; set; }
        public int Quantity { get; set; }
        public int? OrderQuantity { get; set; }
        public int? DeliveriedQuantity { get; set; }
        public int RemainQuantity
        {
            get
            {
                return (OrderQuantity ?? 0) - (DeliveriedQuantity ?? 0);
            }
        }
        public decimal UnitPrice { get; set; }
        public decimal TotalAmount { get; set; }
        public string Description { get; set; }
    }
}
